package com.guoxue.action.common;

import java.text.SimpleDateFormat;
import java.util.Date;
import com.aliyun.oss.common.utils.DateUtil;
import org.springframework.stereotype.Component;
@Component
public class CreatePolicy {
	/**
	 * 处理get请求时候生成rul地址时使用的policy
	 * @param fileName
	 * @param bucket
	 * @param time
	 * @return
	 */
	public String  getPolicy(String fileName,String bucket,Long time){
		String policy="";//定义policy为空
		Date dest = new Date(System.currentTimeMillis() + time);
		//时间拼接，必须小于15分钟
		String str = DateUtil.formatAlternativeIso8601Date(dest);
		//String str = DateUtil.formatIso8601Date(dest);
		//限制条件：1.str过期时间2.bucket规定空间3.fileName规定文件名
		String resource="                \"acs:oss:*:*:"+bucket+"\",\"acs:oss:*:*:"+bucket+"/*\"\n";
		if(!"".equals(fileName)){
			resource="                \"acs:oss:*:*:"+bucket+"\",\"acs:oss:*:*:"+bucket+"/"+fileName+"\"\n";
		}
			policy = "{\n" +
					    "\"expiration\": \""+str+"\","+
			            "    \"Version\": \"1\", \n" +
			            "    \"Statement\": [\n" +
			            "        {\n" +
			            "            \"Action\": [\n" +
			            "                \"oss:List*\", \n" +
			            "                \"oss:Get*\" \n" +
			            "            ], \n" +
			            "            \"Resource\": [\n" +
			            resource+
			            "            ], \n" +
			            "            \"Effect\": \"Allow\"\n" +
			            "        }\n" +
			            "    ]\n" +
			            "}";		
		return policy;
	}
	
	
	/**
	 * 处理上传是的限制Policy
	 * @param fileName 要上传的文件名称
	 * @param limitSize 要上传的文件的最小大小（预留字段）
	 * @param size     要上传的文件大小
	 * @param bucket   要上传的文件指定的bucket
	 * @param time     服务器生成token后的过期时间
	 * @return
	 */
	public String  getPolicy(String fileName,int limitSize,int size,String bucket,Long time){
		String policy="";//定义policy为空
		Date dest = new Date(System.currentTimeMillis() + time);
		//时间拼接
		String str = DateUtil.formatAlternativeIso8601Date(dest);
		//限制条件：1.str过期时间2.bucket规定空间3.fileName规定文件名4.limitSize,size控制文件的最大和最小限制
<<<<<<< HEAD
		policy="{"+
			"\"expiration\": \""+str+"\","+
			"\"conditions\": [ {\"bucket\": \""+bucket+"\" },"+
			"[\"eq\", \"$key\", \""+fileName+"\"],"+
			"[\"content-length-range\", "+limitSize+", "+size+"]"+
			"]}";
=======
		policy = "{"
				+ "\"expiration\":\""+str+"\","
				+ "\"conditions\":[{\"bucket\": \""+bucket+"\" },"
				+ "[\"content-length-range\", "+limitSize+", "+size+"],"
				+ "[\"eq\", \"$key\", \""+fileName+"\"]"
				+ "]}";	
>>>>>>> refs/heads/insideCode
		return policy;
	}
}
